package cn.itsource.quartz.config;

import cn.itsource.basic.constant.BusinessConstant;
import cn.itsource.order.domain.ProductOrder;
import cn.itsource.order.service.IProductOrderService;
import cn.itsource.pay.domain.PayBill;
import cn.itsource.pay.service.IPayBillService;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

public class MainJob implements Job {

    @Autowired
    private IPayBillService payBillService;

    @Autowired
    private IProductOrderService orderService;
    /*
        更改订单的状态

     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //1.获取定时任务的参数
        JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        Map<String,Object> map = (Map<String, Object>) jobDataMap.get("params");
        //2.获取订单号
        String orderSn = (String) map.get("orderSn");
        //3.根据订单号查询 支付单
        PayBill payBill = payBillService.loadByOrderSn(orderSn);
        //4.如果状态为0 改为-1
        if (payBill.getState()==0){
            payBill.setState(-1);//订单取消的状态
            payBill.setUpdateTime(new Date());
        }
        payBillService.update(payBill);
        //5.根据支付单的业务类型 和业务键 找到对应的订单
        switch (payBill.getBusinessType()){
            case BusinessConstant.PRODUCT_ORDER://服务订单
                ProductOrder productOrder = orderService.findOne(payBill.getBusinessKey());
                //6.如果状态为0 改为-1
                if(productOrder.getState()==0){
                    productOrder.setState(-1);
                    orderService.update(productOrder);
                }
                break;
            case  BusinessConstant.GOODS_ORDER://商品订单

                break;

        }


    }


}
